// pages/craftsman_index/craftsman_index.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getStoreInfo, getRelationGoods, setCollectItem, getDistanceLen, getEvaList, getCategoryList } from '../../static/api/api.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        category: null,
        curCatgoryId:'',
        isCollected:0,
        curShopId:'',
        mainData:null,
        curType:10,
        listData: null,
        isNoMore: false,
        pageLoad: false,
        curStatus: 1,
        usableType: 30,
        sort_field:'oie.id',
        pages: {
            curPage: 1,
            totalPage: 1
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'curShopId':options.id
        })

        this.getCategoryList1()
        
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if(this.data.curType==10){
            this.getStoreInfo()
        }
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '"'+this.data.mainData.store_info.store_name + '"这家店服务很棒，我只介绍给你哟！',
            path: '/pages/shop_detail_index/shop_detail_index?id=' + this.data.curShopId,
            imageUrl: this.data.mainData.store_info.main_img
        }
    },
    //获取分类数据
    getCategoryList1() {
        let that = this;
        console.log('加载分类')
        getCategoryList({}).then(response => {
            console.log('分类数据')
            wx.hideLoading()
            that.setData({
                'category': response.data,
                'curCatgoryId': response.data[0].category_id
            })
            this.getStaffWorkList()
        }).catch(response => {
            wx.hideLoading()
            console.log('数据获取失败')
            console.log(response)
        })
    },
    changeCatgory(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            curCatgoryId: dataset.id,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        this.getRelationGoods()
    },
    goUrl(e) {
        utils.goNavigationUrl(e)
    },
    changeType(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            'curType': dataset.type
        })
        this.setData({
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        if (this.data.curType == 20) {

            this.getEvaList()
        }
        if (this.data.curType == 10) {

            this.getRelationGoods()
        }
    },
    getDistanceLen() {
        let loc = wx.getStorageSync('locationData')
        let options = {
            longitude1: this.data.mainData.store_info.longitude,
            latitude1: this.data.mainData.store_info.latitude,
            longitude2: loc.poi.location.lng,
            latitude2: loc.poi.location.lat,
            unit: 'km'
        }
        getDistanceLen(options).then(response => {
            this.setData({
                'distance': (response.data.distance / 1000).toFixed(2),
            })

        }).catch(response => {
        })
    },
    getStoreInfo(){
        let options={
            store_id: this.data.curShopId
        }
        wx.showLoading({
            title: '加载中...',
        })
        getStoreInfo(options).then(response => {
            wx.hideLoading();
            response.data.store_info.other_img = utils.formatImgArr(response.data.store_info.other_img)
            this.setData({
                mainData:response.data,
                'curShopId': response.data.store_info.id,
                'isCollected': response.data.is_behevior?'1':'0'
            })
            this.getRelationGoods()
            this.getDistanceLen()
        }).catch(response => {
            wx.hideLoading();
        })
    },
    setCollectItem() {
        let options = {
            item_id: this.data.curShopId,
            behavior:10
        }
        wx.showLoading({
            title: '加载中...',
        })
        setCollectItem(options).then(response => {
            wx.hideLoading();
            if (response.data.status == 1){
                wx.showToast({
                    icon: 'none',
                    title: '收藏成功',
                })
            }else{
                wx.showToast({
                    icon: 'none',
                    title: '取消收藏成功',
                }) 
            }
            
            this.setData({
                isCollected:response.data.status
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    lower(){
        this.getRelationGoods()
    },
    getRelationGoods() {
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let that = this;
        let params = {
            store_id: this.data.curShopId,
            page:this.data.pages.curPage,
            category_id:this.data.curCatgoryId
        }
        getRelationGoods(params).then(response => {
            console.log(response.data)

            let data = response.data;
            if (that.data.pages.curPage == 1) {
                that.setData({
                    listData: data.lists
                })
            } else {
                that.setData({
                    listData: that.data.listData.concat(data.lists)
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.page.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            that.setData({
                'pageLoad': false
            })
        })

    },
    evalower() {
        this.getEvaList()
    },
    getEvaList() {
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let that = this;
        let params = {
            store_id: this.data.curShopId,
            sort_field: this.data.sort_field,
            sort_type:'desc',
            page: this.data.pages.curPage,
        }
        getEvaList(params).then(response => {
            let data = response.data;
            if (that.data.pages.curPage == 1) {
                that.setData({
                    listData: data.lists
                })
            } else {
                that.setData({
                    listData: that.data.listData.concat(data.lists)
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.page.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            that.setData({
                'pageLoad': false
            })
        })

    },
    //预览图片
    handleImagePreview(e) {
        const dataset = e.currentTarget.dataset
        console.log(dataset)
        console.log(e)
        wx.previewImage({
            current: dataset.src,  //当前预览的图片
            urls: dataset.images,  //所有要预览的图片
        })
    },
    changeEvaType(e){
        const dataset = e.currentTarget.dataset
        this.setData({
            'sort_field':dataset.key
        }) 
        this.setData({
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        this.getEvaList()
    },
    makePhone(e){
        let dataset = e.currentTarget.dataset;
        wx.makePhoneCall({
            phoneNumber: dataset.phone,
        })
    }
})